package com.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.pojo.*;
import com.system.server.CourseService;
import com.system.server.TypeService;
import com.system.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年04月09日16:51
 * @Description: <描述>
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    private TypeService typeService;
    @Resource
    private CourseService courseService;

    @ModelAttribute("roleName")
    public String userType(HttpSession session) {
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
        System.out.println(userLogin);

        Role role = typeService.findRoleByUserLoginName(userLogin.getUsername());
        System.out.println(role);

        String roleName = role.getRoleName();

        return roleName;
    }
    @RequestMapping("/findAll")
    public String showCourse(Integer pageNum,Model model) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        //分页插件
        PageHelper.startPage(pageNum,5);

        List<Course> lists = courseService.findAll();

        PageInfo<Course> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);
        return "/student/showCourse";
    }
    @RequestMapping("/selectCourseByName")
    public String selectCourseByName(String findByName,Integer pageNum,Model model) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);

        List<Course> lists = courseService.findCourseByName(findByName);

        PageInfo<Course> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);
        //数据回显
        model.addAttribute("conditionName",findByName);
        return "/student/showCourse";
    }
    @RequestMapping("/passwordPage")
    public String passwordPage() {
        return "/student/passwordRest";
    }
    @RequestMapping("/overCourse")
    public String overCourse(Model model,HttpSession session,Integer pageNum) {
        if(StringUtils.isEmpty(pageNum)) {
            pageNum = 1;
        }
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");

        PageHelper.startPage(pageNum,5);
        int studentID = Integer.parseInt(userLogin.getUsername());
        List<Course> lists = courseService.findOverSelectedCourse(studentID);

        PageInfo<Course> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);

        return "/student/overCourse";
    }

    @RequestMapping("/selectedCourse")
    public String selectCourse(Model model,HttpSession session,Integer pageNum) {
        if(StringUtils.isEmpty(pageNum)) {
            pageNum = 1;
        }
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");

        PageHelper.startPage(pageNum,5);
        int studentID = Integer.parseInt(userLogin.getUsername());
        List<Course> lists = courseService.findSelectedCourse(studentID);

        PageInfo<Course> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);
        return "/student/selectCourse";
    }
    //退课
    @RequestMapping("/outCourse")
    public String outCourse(Integer courseID,HttpSession session,Model model) {
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
        System.out.println(userLogin+":"+courseID);

        try {
            courseService.outCourse(courseID,userLogin.getUsername());
        } catch (Exception e) {
            model.addAttribute("errorMsg","删除失败");
        }
        return "redirect:/student/selectedCourse";
    }

}
